<?php	
	// Interface function	
	function oh_module_template($tokens) {
		global $oh_object_template;
		if (count($tokens)>0) {
			if ($tokens[0] == "url") {			
				echo $oh_object_template->template_url();
			} else {
				include($oh_object_template->templates['run']."/".$tokens[0].".php");
			}
		}
		return $oh_object_template;
	} 

	// Option file required
	//require_once($oh['path']."/modules/mysql/ezsql/shared/ez_sql_core.php");
	//require_once($oh['path']."/modules/mysql/ezsql/mysql/ez_sql_mysql.php");
	
	// Main class of module
	class oh_class_template {
		public $templates;
		function oh_class_template() {
			$this->templates = array(
				'directory' => array(
					oh("path.script").'/themes'
				),
				'run' => ""
			);
		}
		function run($template,$page_tokens) {
			// retrive file by "pagemanager" rules
			$path = $this->template_path($template);
			for($i=0;$i<count($page_tokens);$i++) {				
				$file = $path."/".implode("-",$page_tokens).".php";
				if (file_exists($file)) {
					return $this->run_file($file);
				} else {
					$temp = array_pop($page_tokens);
					$file = $path."/".implode("-",$page_tokens).".php";				
					if (file_exists($file)) {
						return $this->run_file($file);
					} else {
						return $this->run_file($path."/404.php");					
					}
				}		
			}
			return $this->run_file($path."/index.php");
		}
		function run_file($file) {
			$dir = dirname($file);
			$this->templates['run'] = $dir;
			include($file);
		}
		function template_url() {
			global $oh_object_template;
			return oh("config.site.url").'themes/'.basename($oh_object_template->templates['run']);
		}
		function template_path($template) {
			global $oh_object_template;
			return oh("path.script").'/themes/'.$template;
		}

	}
	
	// Main object of module
	global $oh_object_template;
	$oh_object_template = new oh_class_template();
?>
